<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>画一条线段</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.imageline" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">imageline</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">imageline</span> &mdash; <span class="dc-title">画一条线段</span></p>

 </div>
 <div class="refsect1 unknown-notet" id="refsect1-function.imageline-unknown-notet">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>imageline</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$image</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$x1</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$y1</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$x2</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$y2</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$color</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>imageline()</strong></span> 用 <code class="parameter">color</code>
   颜色在图像 <code class="parameter">image</code> 中从坐标
   <code class="parameter">x1</code>，<code class="parameter">y1</code> 到
   <code class="parameter">x2</code>，<code class="parameter">y2</code>（图像左上角为
   0, 0）画一条线段。
  </p>
  <div class="example" id="example-3721">
   <p><strong>Example #1 画一条粗线</strong></p>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">imagelinethick</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$thick&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;下面两行只在线段直角相交时好使<br />&nbsp;&nbsp;&nbsp;&nbsp;imagesetthickness($image,&nbsp;$thick);<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;imageline($image,&nbsp;$x1,&nbsp;$y1,&nbsp;$x2,&nbsp;$y2,&nbsp;$color);<br />&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$thick&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">imageline</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$t&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$thick&nbsp;</span><span style="color: #007700">/&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">0.5</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$x1&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$x2&nbsp;</span><span style="color: #007700">||&nbsp;</span><span style="color: #0000BB">$y1&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$y2</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">imagefilledrectangle</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">min</span><span style="color: #007700">(</span><span style="color: #0000BB">$x1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x2</span><span style="color: #007700">)&nbsp;-&nbsp;</span><span style="color: #0000BB">$t</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">min</span><span style="color: #007700">(</span><span style="color: #0000BB">$y1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y2</span><span style="color: #007700">)&nbsp;-&nbsp;</span><span style="color: #0000BB">$t</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">max</span><span style="color: #007700">(</span><span style="color: #0000BB">$x1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x2</span><span style="color: #007700">)&nbsp;+&nbsp;</span><span style="color: #0000BB">$t</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">max</span><span style="color: #007700">(</span><span style="color: #0000BB">$y1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y2</span><span style="color: #007700">)&nbsp;+&nbsp;</span><span style="color: #0000BB">$t</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$k&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">$y2&nbsp;</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">$y1</span><span style="color: #007700">)&nbsp;/&nbsp;(</span><span style="color: #0000BB">$x2&nbsp;</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">$x1</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//y&nbsp;=&nbsp;kx&nbsp;+&nbsp;q<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$t&nbsp;</span><span style="color: #007700">/&nbsp;</span><span style="color: #0000BB">sqrt</span><span style="color: #007700">(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">pow</span><span style="color: #007700">(</span><span style="color: #0000BB">$k</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$points&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">$x1&nbsp;</span><span style="color: #007700">-&nbsp;(</span><span style="color: #0000BB">1</span><span style="color: #007700">+</span><span style="color: #0000BB">$k</span><span style="color: #007700">)*</span><span style="color: #0000BB">$a</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">$y1&nbsp;</span><span style="color: #007700">+&nbsp;(</span><span style="color: #0000BB">1</span><span style="color: #007700">-</span><span style="color: #0000BB">$k</span><span style="color: #007700">)*</span><span style="color: #0000BB">$a</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">$x1&nbsp;</span><span style="color: #007700">-&nbsp;(</span><span style="color: #0000BB">1</span><span style="color: #007700">-</span><span style="color: #0000BB">$k</span><span style="color: #007700">)*</span><span style="color: #0000BB">$a</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">$y1&nbsp;</span><span style="color: #007700">-&nbsp;(</span><span style="color: #0000BB">1</span><span style="color: #007700">+</span><span style="color: #0000BB">$k</span><span style="color: #007700">)*</span><span style="color: #0000BB">$a</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">$x2&nbsp;</span><span style="color: #007700">+&nbsp;(</span><span style="color: #0000BB">1</span><span style="color: #007700">+</span><span style="color: #0000BB">$k</span><span style="color: #007700">)*</span><span style="color: #0000BB">$a</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">$y2&nbsp;</span><span style="color: #007700">-&nbsp;(</span><span style="color: #0000BB">1</span><span style="color: #007700">-</span><span style="color: #0000BB">$k</span><span style="color: #007700">)*</span><span style="color: #0000BB">$a</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">$x2&nbsp;</span><span style="color: #007700">+&nbsp;(</span><span style="color: #0000BB">1</span><span style="color: #007700">-</span><span style="color: #0000BB">$k</span><span style="color: #007700">)*</span><span style="color: #0000BB">$a</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">$y2&nbsp;</span><span style="color: #007700">+&nbsp;(</span><span style="color: #0000BB">1</span><span style="color: #007700">+</span><span style="color: #0000BB">$k</span><span style="color: #007700">)*</span><span style="color: #0000BB">$a</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">imagefilledpolygon</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$points</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">imagepolygon</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$points</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

  </div>
  <p class="para">
   参见 <span class="function"><a href="imagecreatetruecolor.html" class="function">imagecreatetruecolor()</a></span> 和
   <span class="function"><a href="imagecolorallocate.html" class="function">imagecolorallocate()</a></span>。
  </p>
 </div>

</div></div></div></body></html>